///////////////////////////////////////////////////////////////////////////////
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
///////////////////////////////////////////////////////////////////////////////
using System;
using System.ComponentModel;
using System.Windows.Data;

using Microsoft.Practices.Composite.Presentation.Commands;
using Microsoft.Practices.Composite.Events;

using DominionModule.Models;
using DominionModule.Services;

namespace DominionModule.ViewModels
{
    /// <summary>
    /// ViewModel for View1.
    /// </summary>
    public class UniformCardStackModel : INotifyPropertyChanged
    {
        private readonly IDataService _dataService;
        private readonly IEventAggregator _eventAggregator;
        private readonly DataItems _model;

        public UniformCardStackModel(IDataService dataService, IEventAggregator eventAggregator)
        {
            _dataService = dataService;
            _eventAggregator = eventAggregator;

            // Get the data model from the data service.
            _model = _dataService.GetModel();

            // Initialize the CollectionView for the underlying model
            // and track the current selection.
            DataItemsCV = new ListCollectionView(_model);
            DataItemsCV.CurrentChanged += new EventHandler(SelectedItemChanged);

            // Initialize this ViewModel's commands.
            Command1 = new DelegateCommand<string>(ExecuteCommand1, CanExecuteCommand1);
        }

        #region DataItems CollectionView

        public ICollectionView DataItemsCV { get; private set; }

        private void SelectedItemChanged(object sender, EventArgs e)
        {
            // Update the command status.
            Command1.RaiseCanExecuteChanged();
        }
        #endregion

        #region Command1
        public DelegateCommand<string> Command1 { get; private set; }

        private void ExecuteCommand1(string commandParameter)
        {
            DataItem item = DataItemsCV.CurrentItem as DataItem;
            if (item != null)
            {
                // Perform an operation on currently selected item.
                // For example: _model.Remove( item );
                DataItemsCV.Refresh();
            }
        }

        private bool CanExecuteCommand1(string commandParameter)
        {
            // Command is only enabled when an item is selected.
            return DataItemsCV.CurrentItem != null;
        }

        #endregion

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
